Skip to content

gh-140746: Fix Thread.start() that can hang indefinitely#140799

Open
ryv-odoo wants to merge 12 commits intopython:mainfrom
ryv-odoo:main-fix-140746-threading-start-waiting
Open

gh-140746: Fix Thread.start() that can hang indefinitely#140799
ryv-odoo wants to merge 12 commits intopython:mainfrom
ryv-odoo:main-fix-140746-threading-start-waiting

Conversation

@ryv-odoo
Copy link

@ryv-odoo ryv-odoo commented Oct 30, 2025

When a new thread is started (Thread.start()), the current thread waits for the new thread's _started signal (self._started.wait()). If the new thread doesn't have enough memory, it might crash before signaling its start to the parent thread, causing the parent thread to wait indefinitely (still in Thread.start()).

To fix the issue, remove the _started python attribute from the Thread class and converted the logic at C level (PyEvent). A flaw of this method, it that the threading module will still contains the zombie thread into the _limbo dictionnary.

We also change Thread._delete() to use pop to remove the thread from _active, as there is no guarantee that the thread exists in _active[get_ident()], thus avoiding a potential KeyError. This can happen if _bootstrap_inner crashes before _active[self._ident] = self executes. We use self._ident because we know set_ident() has already been called.

Moreover, remove the old comment in _delete because _active_limbo_lock became reentrant in commit 243fd01.

Not sure if this fix need/can to be backported

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants